/**
 * 闲聊登录按钮控件
 */
var XianLiaoLoginBtnUnit = LoginBtnUnit.extend({
    _reciveCodeKey: EventKey.ReceiveXianliaoCode,
    _authReq: 'XianLiaoCode',
    _ccsFileName: res.XianLiaoBtnUnit_json,
    redirectLogin: function () {
        LoginBtnUnit.prototype.redirectLogin.call(this);
        showLoading('拉取闲聊授权中..');
        var openid = cc.sys.localStorage.getItem('xl_openid');
        if (isNullString(openid)) {
            XianLiaoUtil.redirectLogin();
        } else {
            var response = {
                openid: cc.sys.localStorage.getItem('xl_openid'),
                nickname: decodeURIComponent(cc.sys.localStorage.getItem('nickname')),
                sex: cc.sys.localStorage.getItem('sex'),
                province: cc.sys.localStorage.getItem('province'),
                city: cc.sys.localStorage.getItem('city'),
                country: cc.sys.localStorage.getItem('country'),
                headimgurl: cc.sys.localStorage.getItem('headimgurl'),
                unionid: cc.sys.localStorage.getItem('unionid')
            };
            this.doLogin(response, 'xl');
        }
    },
    _receiveCode: function (code) {
        LoginBtnUnit.prototype._receiveCode.call(this);
        if (isNullString(code)) {
            showAlert('闲聊授权code错误，请重新登录');
            return;
        }
        this._requestAuth(code);
    },
    _requestAuthSuccess: function (response) {
        var userInfo = JSON.parse(response);
        switch (userInfo.result) {
            case 0:
                cc.sys.localStorage.setItem('xl_openid', userInfo.xianliao.openId);
                cc.sys.localStorage.setItem('nickname', encodeURIComponent(userInfo.xianliao.nick));
                cc.sys.localStorage.setItem('headimgurl', userInfo.xianliao.smallPic + "#");
                cc.sys.localStorage.setItem('sex', userInfo.sex);
                cc.sys.localStorage.setItem('province', userInfo.province);
                cc.sys.localStorage.setItem('city', userInfo.city);
                cc.sys.localStorage.setItem('country', userInfo.country);
                cc.sys.localStorage.setItem('unionid', userInfo.unionid);
                userInfo.nickname = userInfo.xianliao.nick;
                userInfo.headimgurl = userInfo.xianliao.smallPic + "#";
                userInfo.openid = userInfo.xianliao.openId;
                this.doLogin(userInfo, 'xl');
                break;
            case 2:
                WeChatUtil.redirectLogin("Data", userInfo.data);
                break;
            default:
                hideLoading();
                cc.sys.localStorage.removeItem('xl_openid');
                showAlert('闲聊授权验证失败!');
                break;
        }
    },
    _requestAuthFailure: function () {
        LoginBtnUnit.prototype._requestAuthFailure.call(this);
        cc.sys.localStorage.removeItem('xl_openid');
        showAlert('拉取闲聊授权失败,请重新登录!');
    },

    /**
     * 是否安装
     * @returns {boolean}
     * @protected
     */
    _isInstalled: function () {
        return XianLiaoUtil.isInstalled();
    },
    /**
     * 未安装提示
     */
    showNoInstalledAlert: function () {
        showAlert('您没安装闲聊!');
    }
});